package team.cloud.k8s;

import io.fabric8.kubernetes.api.model.Node;
import io.fabric8.kubernetes.api.model.NodeList;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;

@Component
public class K8sNodes {

    private static final Logger logger = LogManager.getLogger(K8sNodes.class);

    public NodeList listNodes(KubernetesClient client) {
        try {
            NodeList list = client.nodes().list();
            logger.info("listNodes成功");
            return list;
        } catch (KubernetesClientException e) {
            logger.error("listNodes失败");
            logger.error("错误信息：", e.getMessage());
            e.printStackTrace();
            return null;
        }
    }

    public Node getNode(KubernetesClient client, String nodeName) {
        try {
            logger.info("getNode成功");
            return client.nodes().withName(nodeName).get();
        } catch (KubernetesClientException e) {
            logger.error("getNode失败");
            logger.error("错误信息：", e.getMessage());
            return null;
        }
    }
}
